// commonFunctions.js
import Decimal from 'decimal.js'

export const sumValues = (list: any[]) => {
  const total = {
    billAmount: '',
    interestDeduction: '',
    rebate: ''
  }
  list.forEach((item: any, _) => {
    const billAmountDecimal = new Decimal(item.billAmount || 0)
    const BillAmount = new Decimal(total.billAmount || 0)
    const rebateDeduction = new Decimal(total.rebate || 0)
    const InterestDeduction = new Decimal(total.interestDeduction || 0)
    const openDecimal = new Decimal(item.open || 0)
    const points = new Decimal(item.points || 0)
    total.billAmount = billAmountDecimal
      .mul(10000)
      .mul(openDecimal)
      .plus(BillAmount)
      .toString()
    total.interestDeduction = points
      .mul(openDecimal)
      .mul(billAmountDecimal)
      .div(100)
      .mul(10000)
      .plus(InterestDeduction)
      .toString()
    total.rebate = points
      .mul(openDecimal)
      .mul(billAmountDecimal)
      .div(100)
      .mul(10000)
      .plus(rebateDeduction)
      .toString()
  })

  return total
}

// #region 计算结果后去掉多余的0
// 计算结果后去掉多余的0
// 自定义函数，去掉末尾不必要的 0 和小数点
export const removeTrailingZeros = (value) => {
  let strValue = new Decimal(value).toString() // 转换为字符串
  // 如果小数点后全是 0，则去掉小数点及之后的部分
  if (strValue.indexOf('.') !== -1) {
    strValue = strValue.replace(/\.?0+$/, '')
  }
  return strValue
}
// #endregion
